//
//  TTJieTuoView.m
//  TaoTuo
//
//  Created by admin on 2019/8/21.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTJieTuoView.h"
#import "TTFiltrateView.h"
#import "LLTagView.h"
#import "LLTagConfig.h"
#import "TTSearchInsertModel.h"
#import "TTSearchModel.h"
#import "TTSearchHistoryView.h"

@interface TTJieTuoView ()<UITextFieldDelegate>
{
    CGFloat viewH;
    CGFloat tagViewH;
}

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) TTScopeView *lbView; // 类别
@property (nonatomic, strong) TTFiltrateView *lbFilteView;
@property (nonatomic, strong) TTScopeView *cpView;  // 产品
@property (nonatomic, strong) TTFiltrateView *cpFilteView;
@property (nonatomic, strong) TTScopeView *colorView;  // 颜色
@property (nonatomic, strong) TTFiltrateView *colorFilteView;
@property (nonatomic, strong) TTScopeView *jdView;  // 精度
@property (nonatomic, strong) TTFiltrateView *jdFilteView;
@property (nonatomic, strong) TTScopeView *cutView;  // 切工
@property (nonatomic, strong) TTFiltrateView *cutFilteView;
@property (nonatomic, strong) TTFiltrateView *xkFilteView;// 镶口
@property (nonatomic, strong) UILabel *luoshiSizeLabel; // 裸石大小
@property (nonatomic, strong) TTFiltrateView *scFilteView;// 手寸
@property (nonatomic, strong) TTFiltrateView *jzFilteView;// 金重
@property (nonatomic, strong) TTFiltrateView *szFilteView; // 手镯
@property (nonatomic, strong) UILabel *szDiameterLabel; // 手掌直径
@property (nonatomic, strong) TTScopeView *czView; // 材质
@property (nonatomic, strong) TTFiltrateView *czFilteView;
@property (nonatomic, strong) TTScopeView *gyView; // 工艺
@property (nonatomic, strong) TTFiltrateView *gyTopFilteView;
@property (nonatomic, strong) TTFiltrateView *gyBottomFilteView;
@property (nonatomic, strong) TTScopeView *jbView; // 戒臂
@property (nonatomic, strong) TTFiltrateView *jbFilteView;
@property (nonatomic, strong) TTScopeView *fsView; // 副石
@property (nonatomic, strong) TTFiltrateView *fsFilteView;
@property (nonatomic, strong) TTScopeView *hotView;
@property (nonatomic, strong) UIButton *moreBtn;

@property (nonatomic, strong) LLTagView *tagView;     // 热门搜索

@property (nonatomic, strong) NSArray *filteViewsArray;
@property (nonatomic, strong) NSArray *scopeViewArray;

@property (nonatomic, assign) BOOL isShowSc;    // 是否要显示手寸
@property (nonatomic, assign) BOOL isSz;    // 选中了手镯
@property (nonatomic, strong) TTSearchHistoryView *historyView;



@end

@implementation TTJieTuoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        self.backgroundColor = [UIColor whiteColor];
        self.searchModel = [[TTSearchModel alloc] init];
        self.searchModel.sourceType = @(1);
        
        _searchNameDic = [NSMutableDictionary dictionary];
        
        viewH = 40;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideAction) name:UIKeyboardWillHideNotification object:nil];
        
    }
    return self;
}

- (void)resetView
{
    self.searchModel = nil;
    self.searchModel = [[TTSearchModel alloc] init];
    self.searchModel.sourceType = @(1);
    
    for (TTScopeView *sv in self.scopeViewArray) {
        [sv resetView];
    }
    for (TTFiltrateView *fv in self.filteViewsArray) {
        [fv resetView];
    }
    [self.lbFilteView setSelectedFirstItem];
    [self.tagView resetView];
    
    self.gyBottomFilteView.dataArray = @[];
    
    [self.searchNameDic removeAllObjects];
    [self setSearchNameDicWithKey:@"0" value:@"女戒"];
    self.searchTF.text = nil;
    [self setSearchNameDicWithKey:@"11" value:nil];
    
    [self.cpFilteView defaultSelectBeforeIndex:1];    // 默认选中前两个
    
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.scView.hidden = !self.isShowSc;
    self.scFilteView.hidden = !self.isShowSc;
    
    self.szView.hidden = !self.isSz;
    self.szFilteView.hidden = !self.isSz;
    self.szDiameterLabel.hidden = self.szFilteView.hidden;
    
    CGFloat xkTop = self.colorView.hidden ? self.cpFilteView.bottom : self.cutFilteView.bottom;
    self.xkView.top = xkTop;
    self.xkFilteView.top = self.xkView.bottom;
    self.luoshiSizeLabel.top = _xkFilteView.top - 8;
    
    self.scView.top = self.xkFilteView.bottom;
    self.scFilteView.top = self.scView.bottom;
    
    self.szView.top = self.xkFilteView.bottom;
    self.szFilteView.top = self.szView.bottom;
    self.szDiameterLabel.top = _szFilteView.top - 8;
    // 金重
    CGFloat jzTop = (self.isShowSc || self.isSz) ? self.scFilteView.bottom : self.xkFilteView.bottom;
    self.jzView.frame = CGRectMake(0, jzTop, self.width, viewH);
    self.jzFilteView.frame = CGRectMake(0, _jzView.bottom, self.width, viewH);

    self.czView.frame = CGRectMake(0, self.jzFilteView.bottom, self.width, viewH - 8);
    self.czFilteView.frame = CGRectMake(0, _czView.bottom, self.width, viewH);
    
    self.gyView.frame = CGRectMake(0, _czFilteView.bottom, self.width, viewH - 8);
    self.gyTopFilteView.frame = CGRectMake(0, _gyView.bottom, self.width, viewH);
    self.gyBottomFilteView.frame = CGRectMake(0, _gyTopFilteView.bottom, self.width, viewH);
    
    CGFloat top = self.gyBottomFilteView.dataArray.count == 0 ? _gyTopFilteView.bottom : _gyBottomFilteView.bottom;
    self.jbView.frame = CGRectMake(0, top, self.width, viewH);
    self.jbFilteView.frame = CGRectMake(0, _jbView.bottom, self.width, viewH);
    
    self.fsView.frame = CGRectMake(0, _jbFilteView.bottom, self.width, viewH);
    self.fsFilteView.frame = CGRectMake(0, _fsView.bottom, self.width, viewH);
    
    self.hotView.frame = CGRectMake(0, _fsFilteView.bottom, self.width, viewH);
    self.moreBtn.frame = CGRectMake(self.width - 100, _hotView.top, 90, viewH);
    
    CGFloat h = 90;
    if (self.moreBtn.selected) {
        h = tagViewH;
    }
    self.tagView.frame = CGRectMake(0, _hotView.bottom, self.width, h);
    
    [self setScrollViewContentSize];
}


- (void)keyboardWillHideAction
{
    self.historyView.hidden = YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSArray *array = [USER_DEFAULT objectForKey:historySearchContentKey];
    self.historyView.hidden = array.count == 0;
    [self.historyView reloadHistoryView];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    self.historyView.hidden = YES;
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    self.historyView.hidden = NO;
    [self.historyView reloadHistoryView];
    return YES;
}

///搜索
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    self.historyView.hidden = YES;
    [self.historyView saveSearchHistoryWithText:textField.text];
    [self gotoSearchResultControlelrWithText:textField.text];
    
    return YES;
}


- (void)gotoSearchResultControlelrWithText:(NSString *)text
{
    [self setSearchNameDicWithKey:@"11" value:text];
    if (self.clickSearchBlock) {
        self.clickSearchBlock(self, text);
    }
}


- (void)setXkDataArray:(NSArray *)xkArray scDataArray:(NSArray *)scArray jzDataArray:(NSArray *)jzArray szDataArray:(nonnull NSArray *)szArray hotSearchArray:(nonnull NSArray *)hotSearchArray
{
    
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:_scrollView];
    
    _searchTF = [[LLSearchTextField alloc] initWithFrame:CGRectMake(38, 10, self.width - 38 * 2, 30)];
    _searchTF.backgroundColor = UIColorHex(EDEBFF);
    _searchTF.delegate = self;
    _searchTF.placeholder = @"输入 成品/系列/品牌 搜索";
    [self.scrollView addSubview:_searchTF];
    
    @weakify(self)
    [_searchTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        if (x.length == 0) {
            [self.searchNameDic removeObjectForKey:@"11"];
            self.searchModel.searchKey = self.currentSelectHotSearchString;
        } else {
            [self setSearchNameDicWithKey:@"11" value:x];
        }
    }];
    
    
    _lbView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _searchTF.bottom, self.width, viewH - 8) title:@"类别" hiddenTextField:YES];
    [self.scrollView addSubview:_lbView];
    
    _lbFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _lbView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.categoryName = selectedArray.firstObject;
        [self setSearchNameDicWithKey:@"0" value:selectedArray.firstObject];
        if ([selectedArray.firstObject isEqualToString:@"手镯"]) {
            self.isSz = YES;
        } else {
            self.isSz = NO;
            [self.searchNameDic removeObjectForKey:@"5"];
            [self.szFilteView resetView];
            [self.szView setMinStr:nil maxStr:nil];
        }
        // 是否要显示手寸
        self.isShowSc = [@[@"女戒", @"男戒", @"对戒"] containsObject:selectedArray.firstObject];
        if (!self.isShowSc) {
            [self.scFilteView resetView];
            [self.searchNameDic removeObjectForKey:@"3"];
            [self.scView setMinStr:nil maxStr:nil];
        }
        [self setNeedsLayout];
    } leftRightSpace:0];
    NSArray *categoryNamesArray = @[@"女戒", @"套链", @"对戒", @"男戒", @"吊坠", @"手链", @"手镯", @"耳钉", @"耳线", @"耳坠"];
    _lbFilteView.dataArray = categoryNamesArray;
    if ([categoryNamesArray containsObject:self.categoryName]) {
        NSInteger index = [categoryNamesArray indexOfObject:self.categoryName];
        [_lbFilteView setSelectedItemWithIndex:index];
        [_lbFilteView scrollToCenterWithIndex:index];
    } else {
        [_lbFilteView setSelectedFirstItem];    // 默认选中第一个
    }
    
    [self.scrollView addSubview:_lbFilteView];
    
    _cpView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _lbFilteView.bottom, self.width, viewH - 8) title:@"产品" hiddenTextField:YES];
    [self.scrollView addSubview:_cpView];
    
    _cpFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _cpView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        NSNumber *finishedProduct = @0;
        if ([selectedArray.firstObject isEqualToString:@"空托"]) {
            finishedProduct = @1;
        } else if ([selectedArray.firstObject isEqualToString:@"成品"]) {
            finishedProduct = @2;
        }
//        NSNumber *finishedProduct = @0;
//        if (selectedArray.count < 2) {
//            BOOL isKT = [@"空托" isEqualToString:selectedArray.firstObject];
//            finishedProduct = isKT ? @1 : @2;
//        }
        BOOL show = finishedProduct.intValue == 2;
        self.colorView.hidden = !show;
        self.colorFilteView.hidden = !show;
        self.jdView.hidden = !show;
        self.jdFilteView.hidden = !show;
        self.cutView.hidden = !show;
        self.cutFilteView.hidden = !show;
        [self setNeedsLayout];
        if (!show) {
            [self.colorFilteView resetView];
            [self.jdFilteView resetView];
            [self.cutFilteView resetView];
        }
        self.searchModel.finishedProduct = finishedProduct;
        [self setSearchNameDicWithKey:@"1" value:[selectedArray componentsJoinedByString:@" "]];
        
    } leftRightSpace:0];
    _cpFilteView.dataArray = @[@"空托", @"成品"];
    [self.scrollView addSubview:_cpFilteView];
    
    _colorView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _cpFilteView.bottom, self.width, viewH - 8) title:@"主石颜色" hiddenTextField:YES];
    _colorView.hidden = YES;
    [self.scrollView addSubview:_colorView];
    _colorFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _colorView.bottom, self.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.sStoneColor = [selectedArray componentsJoinedByString:@","];
        [self setSearchNameDicWithKey:@"2" value:self.searchModel.sStoneColor];
    } leftRightSpace:0];
    _colorFilteView.dataArray = @[@"D-E", @"F-G", @"H", @"I-J", @"K-L", @"M-N", @"<N", @"黑色", @"白色"];
    _colorFilteView.hidden = YES;
    [self.scrollView addSubview:_colorFilteView];
    
    _jdView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _colorFilteView.bottom, self.width, viewH - 8) title:@"主石净度" hiddenTextField:YES];
    _jdView.hidden = YES;
    [self.scrollView addSubview:_jdView];
    _jdFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _jdView.bottom, self.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.sStoneClarity = [selectedArray componentsJoinedByString:@","];
        [self setSearchNameDicWithKey:@"3" value:self.searchModel.sStoneClarity];
    } leftRightSpace:0];
    _jdFilteView.dataArray = @[@"LC", @"VVS", @"VS", @"SI", @"P"];
    _jdFilteView.hidden = YES;
    [self.scrollView addSubview:_jdFilteView];
    
    _cutView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _jdFilteView.bottom, self.width, viewH - 8) title:@"主石切工" hiddenTextField:YES];
    _cutView.hidden = YES;
    [self.scrollView addSubview:_cutView];
    _cutFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _cutView.bottom, self.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.sStoneCut = [selectedArray componentsJoinedByString:@","];
        [self setSearchNameDicWithKey:@"4" value:self.searchModel.sStoneCut];
    } leftRightSpace:0];
    _cutFilteView.dataArray = @[@"十二心十二箭", @"十心十箭", @"八心八箭", @"很好"];
    _cutFilteView.hidden = YES;
    [self.scrollView addSubview:_cutFilteView];
    
    
    _xkView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _cpFilteView.bottom, self.width, viewH) title:@"镶口" hiddenTextField:NO];
    _xkView.minTF.backgroundColor = UIColorHex(EDEBFF);
    _xkView.maxTF.backgroundColor = UIColorHex(EDEBFF);
    [self.scrollView addSubview:_xkView];
    
    _xkFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _xkView.bottom + 6, self.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        TTSearchInsertModel *minModel = selectedArray.firstObject;
        TTSearchInsertModel *maxModel = selectedArray.lastObject;
        [self.xkView setMinStr:minModel.min maxStr:maxModel.max];
        NSString *str = @"";
        if (maxModel) {
            str = [NSString stringWithFormat:@"%@-%@分",minModel.min, maxModel.max];
        }
        [self setSearchNameDicWithKey:@"5" value:str];
    } leftRightSpace:0];
    _xkFilteView.dataArray = xkArray;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.xkFilteView scrollToIndex:3];
    });
    [self.scrollView addSubview:_xkFilteView];
    
    _luoshiSizeLabel = [UILabel labelWithTitle:@"裸石大小" color:UIColorHex(666666) fontSize:8];
    _luoshiSizeLabel.frame = CGRectMake(10, _xkFilteView.top - 8, 100, 10);
    [self.scrollView addSubview:_luoshiSizeLabel];
    
    _scView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _xkFilteView.bottom, self.width, viewH) title:@"手寸" hiddenTextField:NO];
    _scView.minTF.backgroundColor = UIColorHex(EDEBFF);
    _scView.maxTF.backgroundColor = UIColorHex(EDEBFF);
    [self.scrollView addSubview:_scView];
    _scFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _scView.bottom, self.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        TTSearchInsertModel *minModel = selectedArray.firstObject;
        TTSearchInsertModel *maxModel = selectedArray.lastObject;
        [self.scView setMinStr:minModel.min maxStr:maxModel.max];
        
        NSString *str = @"";
        if (maxModel) {
            str = [NSString stringWithFormat:@"%@-%@#",minModel.min, maxModel.max];
        }
        [self setSearchNameDicWithKey:@"6" value:str];
    } leftRightSpace:0];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.scFilteView scrollToIndex:3];
    });
    _scFilteView.dataArray = scArray;
    [self.scrollView addSubview:_scFilteView];
    
    //TODO: key
    _szView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _xkFilteView.bottom, self.width, viewH) title:@"手镯内径" hiddenTextField:NO];
    _szView.minTF.backgroundColor = UIColorHex(EDEBFF);
    _szView.maxTF.backgroundColor = UIColorHex(EDEBFF);
    _szView.hidden = YES;
    [self.scrollView addSubview:_szView];
    _szFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _szView.bottom, self.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        TTSearchInsertModel *minModel = selectedArray.firstObject;
        TTSearchInsertModel *maxModel = selectedArray.lastObject;
        [self.szView setMinStr:minModel.min maxStr:maxModel.max];
        NSString *str = @"";
        if (maxModel) {
            str = [NSString stringWithFormat:@"%@-%@",minModel.min, maxModel.max];
        }
        [self setSearchNameDicWithKey:@"8" value:str];
    } leftRightSpace:0];
    _szFilteView.dataArray = szArray;
    _szFilteView.hidden = YES;
    [self.scrollView addSubview:_szFilteView];
    _szDiameterLabel = [UILabel labelWithTitle:@"手掌直径" color:UIColorHex(666666) fontSize:8];
    _szDiameterLabel.hidden = YES;
    _szDiameterLabel.frame = CGRectMake(10, _szFilteView.top - 8, 100, 10);
    [self.scrollView addSubview:_szDiameterLabel];
    
    
    _jzView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _scFilteView.bottom, self.width, viewH) title:@"金重" hiddenTextField:NO];
    _jzView.minTF.backgroundColor = UIColorHex(EDEBFF);
    _jzView.maxTF.backgroundColor = UIColorHex(EDEBFF);
    [self.scrollView addSubview:_jzView];
    _jzFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _jzView.bottom, self.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        TTSearchInsertModel *minModel = selectedArray.firstObject;
        TTSearchInsertModel *maxModel = selectedArray.lastObject;
        [self.jzView setMinStr:minModel.min maxStr:maxModel.max];
        NSString *str = @"";
        if (maxModel) {
            str = [NSString stringWithFormat:@"%@-%@g",minModel.min, maxModel.max];
        }
        [self setSearchNameDicWithKey:@"7" value:str];
    } leftRightSpace:0];
    _jzFilteView.dataArray = jzArray;
    [self.scrollView addSubview:_jzFilteView];
    
    
    _czView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _jzFilteView.bottom, self.width, viewH - 8) title:@"材质" hiddenTextField:YES];
    [self.scrollView addSubview:_czView];
    _czFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _czView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.material = selectedArray.firstObject;
        [self setSearchNameDicWithKey:@"9" value:selectedArray.firstObject];
    } leftRightSpace:0];
    _czFilteView.dataArray = @[@"AU750", @"PT950"];
    [self.scrollView addSubview:_czFilteView];
    
    NSDictionary *dic = @{
                          @"爪镶" : @[@"3-4爪", @"5-6爪", @"爪镶其他"],
                          @"包夹镶" : @[@"2-3叶包", @"4-6叶包", @"包夹其他"],
                          @"群镶" : @[]
                          };
    _gyView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _czFilteView.bottom, self.width, viewH - 8) title:@"镶法" hiddenTextField:YES];
    [self.scrollView addSubview:_gyView];
    _gyTopFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _gyView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        NSLog(@"--> %@", selectedArray.firstObject);
        @strongify(self)
        self.searchModel.childProcess = nil;
        [self.searchNameDic removeObjectForKey:@"11"];
        self.gyBottomFilteView.dataArray = dic[selectedArray.firstObject];
        [self.gyBottomFilteView resetView];
        [self setNeedsLayout];
        self.searchModel.process = selectedArray.firstObject;
        if (selectedArray.count > 0) {
            [self setSearchNameDicWithKey:@"10" value:selectedArray.firstObject];
        } else {
            [self.searchNameDic removeObjectForKey:@"10"];
        }
    } leftRightSpace:0];
    _gyTopFilteView.dataArray = @[@"爪镶", @"包夹镶", @"群镶"];
    [self.scrollView addSubview:_gyTopFilteView];
    _gyBottomFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _gyTopFilteView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.childProcess = selectedArray.firstObject;
        [self setSearchNameDicWithKey:@"11" value:selectedArray.firstObject];
    } leftRightSpace:0];
    [self.scrollView addSubview:_gyBottomFilteView];
    
    
    _jbView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _gyTopFilteView.bottom, self.width, viewH - 8) title:@"戒臂" hiddenTextField:YES];
    [self.scrollView addSubview:_jbView];
    _jbFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _jbView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.ringArms = selectedArray.firstObject;
        [self setSearchNameDicWithKey:@"12" value:selectedArray.firstObject];
    } leftRightSpace:0];
    _jbFilteView.dataArray = @[@"直臂", @"扭臂"];
    [self.scrollView addSubview:_jbFilteView];
    
    
    _fsView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _jbFilteView.bottom, self.width, viewH - 8) title:@"副石" hiddenTextField:YES];
    [self.scrollView addSubview:_fsView];
    _fsFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _fsView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        NSString *has = [selectedArray.firstObject isEqualToString:@"有"] ? @"true" : @"false";
        self.searchModel.isExistVStone = has;
        NSString *str = [selectedArray.firstObject isEqualToString:@"有"] ? @"副石" : @"";
        [self setSearchNameDicWithKey:@"13" value:str];
    } leftRightSpace:0];
    _fsFilteView.dataArray = @[@"有", @"无"];
    [self.scrollView addSubview:_fsFilteView];
    
    
    _hotView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _fsFilteView.bottom, self.width, viewH) title:@"热门搜索" hiddenTextField:YES];
    [self.scrollView addSubview:_hotView];
    _moreBtn = [UIButton createButtonWithNormalTitle:@"更多" selectedTitle:@"收起" titleColor:UIColorHex(999999) titleFont:[UIFont systemFontOfSize:12] target:self action:@selector(moreBtnClick:)];
    _moreBtn.frame = CGRectMake(self.width - 100, _hotView.top, 90, viewH);
    [_moreBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [self.scrollView addSubview:_moreBtn];
    
    NSMutableArray *hotArray = [NSMutableArray array];
    for (TTSearchInsertModel *model in hotSearchArray) {
        [hotArray addObject:model.title];
    }
    LLTagConfig *config = [[LLTagConfig alloc] init];
    config.minItemSpace = 5;
    config.minLineSpace = 5;
    config.sectionInset = UIEdgeInsetsZero;
    config.itemHeight = viewH;
    config.tagNormalbackgroundColor = UIColorHex(F0F4F7);
    config.tagSelectbackgroundColor = kMainColor;
    
    // 取出搜索词
    NSDictionary *hotDic = [USER_DEFAULT objectForKey:ad_data_key];
    NSArray *seararray = hotDic[@"SummaryList"];
    if (seararray) {
        hotArray = seararray.mutableCopy;
    }
    
    _tagView = [[LLTagView alloc] initWithFrame:CGRectMake(0, _hotView.bottom, self.width, 90) dataArray:hotArray selectItems:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        self.currentSelectHotSearchString = selectArray.firstObject;
        self.searchModel.searchKey = self.currentSelectHotSearchString;
        [self setSearchNameDicWithKey:@"15" value:selectArray.firstObject];
    }];
    _tagView.canCancelSelect = YES;
    _tagView.backgroundColor = [UIColor whiteColor];
    _tagView.config = config;
    _tagView.allowMutiSelect = NO;
    _tagView.layer.masksToBounds = YES;
    [self.scrollView addSubview:_tagView];
    
    
    self.filteViewsArray = @[_lbFilteView, _cpFilteView, _colorFilteView, _jdFilteView, _cutFilteView, _xkFilteView, _scFilteView, _jzFilteView, _szFilteView, _czFilteView, _gyTopFilteView, _gyBottomFilteView, _jbFilteView, _fsFilteView];
    self.scopeViewArray = @[_xkView, _scView, _jzView, _szView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self->tagViewH = self.tagView.height;
        
        self.tagView.height = 90;
    
        [self setScrollViewContentSize];
        
    });
    
    
    
}

- (void)setScrollViewContentSize
{
    NSInteger count = self.colorFilteView.hidden ? (self.filteViewsArray.count - 3) : self.filteViewsArray.count;
    self.scrollView.contentSize = CGSizeMake(self.width, count * 2 * self->viewH + self.tagView.height + 40);
}

- (void)moreBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    
    self.tagView.height = sender.selected ? tagViewH : 90;
    
    [self setScrollViewContentSize];
}

// 设置搜索名称
- (void)setSearchNameDicWithKey:(NSString *)key value:(NSString *)value
{
    if (value == nil || key == nil) {
        [self.searchNameDic removeObjectForKey:key];
        return;
    }
    NSString *str = value.length > 0 ? value : @"";
    [self.searchNameDic setObject:str forKey:key];
}


#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView
{
    return self;
}

- (TTSearchHistoryView *)historyView
{
    if (!_historyView) {
        _historyView = [[TTSearchHistoryView alloc] initWithFrame:CGRectMake(self.searchTF.left + 10, self.searchTF.bottom + 0.5, self.searchTF.width - 20, 100)];
        _historyView.hidden = YES;
        @weakify(self)
        _historyView.clickSearchHistoryBlock = ^(NSString * _Nonnull text) {
            @strongify(self)
            self.searchTF.text = text;
            [self gotoSearchResultControlelrWithText:text];
        };
        [self.scrollView addSubview:_historyView];
    }
    return _historyView;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
